﻿Imports FinanceiroBLL
Imports FinanceiroDAL

Public Class ParcelamentosFRM
#Region "Variáveis"
    Public tp As String
    Public objfrmcontaspagar As New ContasaPagarFRM
    Public codigo As Integer
    Dim tabItens As New DataTable
    Dim objBLL As New ContasPagarBLL
    Public objitens As ItensCtasPagarBLL
    Dim DiaMes As Date
    Public diavencim As Date
    Public doc As String
    Public numparcelas As Integer
    Dim aux As Integer
    Dim conta, codigodgv As Integer
    Dim ret As Boolean
    Dim pag, st, codctp As Integer
    Dim i As Integer
#End Region
#Region "limpar"
    Sub limpar()
        TXTDias.Text = Nothing
        TXTParcelas.Text = Nothing
        tabItens = New DataTable
        'TXTTitular.Text = Nothing
        'TXTDocumento.Text = Nothing
        'TXTValorTotal.Text = Nothing
    End Sub
#End Region
    Private Sub BBuscar_Click(sender As System.Object, e As System.EventArgs)
        tp = "ret"
        ConsultaPrestadoresFRM.ShowDialog()
    End Sub
#Region "inicializa"
    Sub inicializa()
        TXTTitular.Text = objfrmcontaspagar.sacado
        TXTValorTotal.Text = objfrmcontaspagar.valortotal
        TXTDocumento.Text = doc
        TXTParcelas.Text = numparcelas
        DTPVencimento.Text = diavencim
        objitens = New ItensCtasPagarBLL(codigo, 5)
        codctp = objitens.ctpcodigo
        codigo = objitens.icpcodigo
        pag = objitens.pag_codigo
        conta = objitens.con_codigo
        st = objitens.sta_codigo
        '  CarregaGrid()
    End Sub
#End Region
#Region "Load"
    Private Sub ParcelamentosFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        limpar()
        CarregaGrid()
        inicializa()
    End Sub
#End Region
#Region "Carrega Grid"
    Sub CarregaGrid()
        DGVDados.DataSource = objBLL.localizacod(codigo) '*aqui esta´pegando ctp
        DGVDados.Refresh()
    End Sub
#End Region
#Region "Calculo Dias"
    Sub CalculoDias(ByVal dias As Integer, ByVal vencimento As Date)
        Dim venc As Date = vencimento
        DiaMes = (venc.AddDays(dias))
    End Sub
#End Region
#Region "Fechar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
        limpar()
        objBLL = Nothing
    End Sub
#End Region
#Region "Valida item"
    Function validaitem() As Boolean
        Dim ret As Boolean
        ret = False
        For Each linha As DataRow In tabItens.Rows
            If (linha("ctp_codigo") = Val(LCodigo.Text)) Then
                ret = True
            End If
        Next
        Return ret
    End Function
#End Region
#Region "Soma Grid"
    Sub SomaColunas()
        Dim indice As Integer = -1
        Try
            TXTSoma.Text = objitens.Somaparcelas().ToString
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        Finally
        End Try
    End Sub
#End Region
#Region "KeyPress"
    Private Sub TXTValorTotal_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTValorTotal.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
        If e.KeyChar = Convert.ToChar(Keys.Return) Then
            '  TSBSalvar.Select()
            TXTValorTotal.Text = FormatCurrency(TXTValorTotal.Text.Replace("R$", "").Replace("$", "").Replace("R", "").Replace(" ", ""))
        End If
    End Sub
#End Region
#Region "Leave"
    Private Sub TXTValorTotal_Leave(sender As System.Object, e As System.EventArgs) Handles TXTValorTotal.Leave
        TXTValorTotal.Text = FormatCurrency(TXTValorTotal.Text.Replace("R$", "").Replace("$", "").Replace("R", "").Replace(" ", ""))
    End Sub
#End Region
#Region "Excluir"
    Sub exclui() 'exclui intens contas pagar
        ret = False
        objitens = New ItensCtasPagarBLL

        ret = objitens.excluir(codctp) 'foi trocado este cod
        ' objitens.limpar()
        If (ret = True) Then
        Else
            MsgBox("Houve um problema, verifique o parcelamento", MsgBoxStyle.Exclamation)
        End If
    End Sub
#End Region
#Region "Calcular"
    Private Sub BCalcular_Click(sender As System.Object, e As System.EventArgs) Handles BCalcular.Click

        Dim dias As Integer = Val(TXTDias.Text)
        Dim vencimento As Date = DTPVencimento.Text
        Dim Vlrparc, parc As Decimal
        Dim valortotal As Decimal = TXTValorTotal.Text
        Dim par As Decimal = TXTParcelas.Text
        Try
            If (TXTParcelas.Text <> "" And TXTParcelas.Text <> "0") Then
                exclui()
                If (ret = True) Then
                    If (Val(TXTDias.Text) <> 0) Then
                        Vlrparc = valortotal / par
                        i = 0
                        While i < par
                            Dim Data As Date = vencimento
                            parc = Vlrparc
                            objitens = New ItensCtasPagarBLL(0, codctp, Nothing, parc, pag, Data, 0, 0, 0, i + 1, conta, "", st)
                            objitens.InserirItensCtasPagar()
                            i = i + 1
                            vencimento = vencimento.AddDays(dias) 'add dias + definido
                        End While
                        DGVDados.DataSource = objitens.localizar(codctp, 3)
                        EditaParcelamentoFRM.qtditem = DGVDados.RowCount.ToString
                        DGVDados.Refresh()
                        SomaColunas()
                    Else
                        MsgBox("Preencha o campo dias! ", MsgBoxStyle.Information)
                    End If
                Else
                    MsgBox("Parcelamento não realizado.", MsgBoxStyle.Exclamation)
                End If
            Else
                MsgBox("Campo número de parcelas não preenchido.", MsgBoxStyle.Exclamation)
            End If
        Catch ex As Exception
        End Try
    End Sub
#End Region
#Region "Text Changed"
    Private Sub TXTValorTotal_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTValorTotal.TextChanged
        TXTValorTotal.Text = FormatCurrency(TXTValorTotal.Text.Replace("R$", ""))
    End Sub
#End Region
#Region "Alterar"
    Private Sub TSBAlterar_Click(sender As System.Object, e As System.EventArgs) Handles TSBAlterar.Click
        Dim edt As New EditaParcelamentoFRM
        edt.Op = "P"
        Dim Informacao As Integer
        If DGVDados.Rows.Count > 0 Then
            Informacao = DGVDados.CurrentRow.Cells("ctp_codigo").Value.ToString()
            edt.codctp = Informacao
            edt.ShowDialog()
            edt.Dispose()
            edt = Nothing
            limpar()
            Me.Close()
        Else
            MsgBox("Nenhuma linha encontrada!")
        End If
    End Sub
#End Region
#Region "Key Press"
    Private Sub ParcelamentosFRM_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
        If e.KeyChar = Convert.ToChar(13) Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub

    Private Sub TXTDias_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTDias.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then  'somnete números e backspace
            e.Handled = True
        End If
    End Sub
    Private Sub TXTParcelas_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTParcelas.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then  'somnete números e backspace
            e.Handled = True
        End If
    End Sub
#End Region
#Region "Propriedades"
    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Dim edt As New EditaParcelamentoFRM
        edt.Op = "P"
        Dim Informacao As Integer
        If DGVDados.Rows.Count > 0 Then
            Informacao = DGVDados.CurrentRow.Cells("ctp_codigo").Value.ToString()
            edt.codctp = Informacao
            edt.ShowDialog()
            Me.Close()
        Else
            MsgBox("Nenhuma linha encontrada!")
        End If

    End Sub

    Private Sub DTPVencimento_Validating(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles DTPVencimento.Validating
        If FormatDateTime(DTPVencimento.Text) < (Date.Now).ToShortDateString Then
            MsgBox("Data de vencimento menor que a data atual", MsgBoxStyle.Exclamation)
            DTPVencimento.Select()
        End If
    End Sub
#End Region
#Region "Leave"
    Private Sub DTPVencimento_Leave(sender As System.Object, e As System.EventArgs) Handles DTPVencimento.Leave
        If FormatDateTime(DTPVencimento.Text) < (Date.Now).ToShortDateString Then
            MsgBox("Data de vencimento menor que a data atual", MsgBoxStyle.Exclamation)
            DTPVencimento.Select()
        End If
    End Sub
#End Region
#Region "Salvar"
    Private Sub TSBSAlvar_Click_1(sender As System.Object, e As System.EventArgs) Handles TSBSAlvar.Click
        Dim dias As Integer = Val(TXTDias.Text)
        Dim parcelas As Integer = Val(TXTParcelas.Text)

        Try
            If DGVDados.Rows.Count > 0 Then
                MsgBox("Parcelamento efetuado com sucesso!")
                Me.Close()
                limpar()
            Else
                MsgBox("Dados inexistentes! Verifique os procedimentos")

            End If
        Catch ex As Exception

        End Try

    End Sub
#End Region
End Class